from typing import Union, Tuple, List, Optional
import xml.etree.ElementTree as ET


class IncorrectXML(Exception):
    pass


class PropXMLParser(object):
    def __init__(self, raw: Union[str, bytes]):
        if isinstance(raw, bytes):
            raw = raw.decode("utf-8")

        self.root: Optional[ET.Element] = ET.fromstring(raw) if raw else None

    def find_query_props(self) -> Tuple[List[str], bool, bool]:
        if self.root is None:
            return [], False, True

        props_elem = self.root.find("./{DAV:}prop")
        query_props = []

        if props_elem:
            query_props = [x.tag.replace("{DAV:}", '') for x in props_elem.findall("./")]

        prop_name = True if self.root.find("./{DAV:}propname") is not None else False
        all_prop = True if self.root.find("./{DAV:}allprop") is not None else False

        if bool(query_props) + prop_name + all_prop != 1:
            # 请求给定属性, 请求全部属性名称, 请求所有属性这三种状态有且只有一种被满足
            raise IncorrectXML()

        return query_props, prop_name, all_prop


if __name__ == "__main__":
    pass
